package main;

import Messaging.ContactsSelector;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

//A canvas to diplay all the rates of a specific currency.
//From here the user can send an SMS containing those rates.
public class CurrencyInfoCanvas extends Canvas implements CommandListener
{
    private Display display;
    Command backCmd;
    Command smsCmd;
    
    public Currency currentCurrency;
    
    public CurrencyInfoCanvas(Display d, String currencyToShow)
    {
        display=d;
        currentCurrency=(Currency)(DataHandler.currencies.get(currencyToShow));
        
        backCmd=new Command("Back",Command.BACK,0);
        this.addCommand(backCmd);
        smsCmd=new Command("SMS",Command.OK,1);
        this.addCommand(smsCmd);
        setCommandListener(this);
    }
    
    public void startCanvas(String currencyToShow)
    {
        currentCurrency=(Currency)(DataHandler.currencies.get(currencyToShow));
        display.setCurrent(this);
    }
    
    protected void paint(Graphics g)
    {
        try
        {
            g.drawImage(Image.createImage("/images/back.png"), 0, 0, Graphics.TOP | Graphics.LEFT);
        }
        catch (Exception ex){}
        
        g.setColor(255,255,255);
        g.drawString("1 "+currentCurrency.getName()+" =", getWidth()/2, 0,  Graphics.TOP|Graphics.HCENTER);
        try
        {
            Image image = Image.createImage("/images/"+currentCurrency.getName()+".png");
            g.drawImage(image, 55, 0, Graphics.TOP | Graphics.LEFT);
            
            int textY=40;
            String rateValue="";
            for (int i=0;i<DataHandler.currencyNames.length;i++)
            {
                if (!DataHandler.currencyNames[i].equals(currentCurrency.getName()))
                {
                    rateValue=currentCurrency.getRates()[i]+"";
                    if (rateValue.substring(rateValue.indexOf(".")+1).length()>5)
                    {
                        rateValue=rateValue.substring(0,rateValue.indexOf(".")+1)+rateValue.substring(rateValue.indexOf(".")+1,rateValue.indexOf(".")+6);
                    }
                    g.drawString(rateValue+" "+DataHandler.currencyNames[i], 80, textY,  Graphics.TOP|Graphics.LEFT);
                    g.drawImage(Image.createImage("/images/"+DataHandler.currencyNames[i]+".png"), 65, textY, Graphics.TOP | Graphics.RIGHT);
                    textY+=25;
                }
            }
        }
        catch (IOException ex)
        {
            
        }
    }
    
    
    public void commandAction(Command c, Displayable d)
    {
        if (c==backCmd)
        {
            display.setCurrent(mainMIDlet.canvas);
        }
        else if (c==smsCmd)
        {
            new ContactsSelector(display,this).selectContact();
        }
    }
}